#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/alias.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/print_sys_info.sh"
# APP_INSTALL_NAME="index-tts"
APP_TARGET_DIR_NAME="index-tts"
# INDEX_TTS_ENV_NAME=$APP_INSTALL_NAME

export CURRENT_APP_PATH="$LMD_APPS_DIR/$APP_TARGET_DIR_NAME"
echo "App Dir: ${CURRENT_APP_PATH}"

cd "$CURRENT_APP_PATH"

if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi

export GRADIO_TEMP_DIR="$CURRENT_APP_PATH/temp"
CONDA_PREFIX="./venv"
CONDA_ENV_NAME=index-tts

conda_run_pip_install py-cpuinfo huggingface_hub xet filelock fsspec httpx==0.28.1

LOCAL_MODEL_DIR=""
LOCAL_MODEL_DIR_v1="checkpoints"
LOCAL_MODEL_DIR_v1_5="checkpoints-1.5"
if [ -d "$LOCAL_MODEL_DIR_v1_5" ]; then
    LOCAL_MODEL_DIR=$LOCAL_MODEL_DIR_v1_5
else
    LOCAL_MODEL_DIR=$LOCAL_MODEL_DIR_v1
fi
echo "model dir $LOCAL_MODEL_DIR" 

PORT_PARAM=""
if [ -n "$GRADIO_SERVER_PORT" ]; then
  PORT_PARAM="--port $GRADIO_SERVER_PORT"
fi

if [ -d "$CONDA_PREFIX" ]; then
    echo "use ./venv to start app. "$CONDA_PREFIX
    conda run -p $CONDA_PREFIX --no-capture-output python webui.py $PORT_PARAM --model_dir $LOCAL_MODEL_DIR
else
    echo "no ./venv. use env. "$CONDA_ENV_NAME
    conda run -n $CONDA_ENV_NAME --no-capture-output python webui.py $PORT_PARAM --model_dir $LOCAL_MODEL_DIR
fi
